package com.shopmall.controller.goods;

import com.alibaba.excel.EasyExcel;
import com.shopmall.common.ResponseData;
import com.shopmall.goods.entity.GoodsInfo;
import com.shopmall.goods.entity.GoodsInfoExport;
import com.shopmall.goods.service.GoodsInfoService;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsInfoController extends ResponseData {
    @Autowired
    private GoodsInfoService goodsInfoService;
    @PostMapping("pageList")
    public ResponseData pageList(@RequestBody GoodsInfo data) {
        return super.success(goodsInfoService.pageList(data));
    }

    @PostMapping("addOrUpdate")
    public ResponseData addOrUpdate(@RequestBody GoodsInfo data) {
        return super.success(goodsInfoService.addOrUpdate(data));
    }

    @GetMapping("/dowloadFile")
    public void dowloadFile(HttpServletResponse response) throws IOException {
        String fileName = new String("导出excel.xlsx".getBytes(), StandardCharsets.ISO_8859_1);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName );
        ServletOutputStream outputStream = response.getOutputStream();
        List<GoodsInfoExport> list = new ArrayList<>();
        GoodsInfoExport goods = new GoodsInfoExport();
        goods.setId(1);
        goods.setGoodsName("商品1");
        list.add(goods);
        EasyExcel.write(outputStream, GoodsInfoExport.class).sheet("sheet1").doWrite(list);
    }

}
